Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Change submission logic for regular form posts #853

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

caesay
Copy link
Contributor

@caesay caesay commented Jun 21, 2017

If using onSubmit, it is only called when the form is valid. If using an action instead, the form can be submitted at any time regardless of the validity. This is not consistent.

@caesay
Copy link
Contributor Author

caesay commented Jun 21, 2017

An alternative suggestion would be that you could expose the field state or validity via the onSubmit or onBeforeSubmit callback so we can make a decision ourselves. As it stands currently, there's no way to stop a form submission based on its validity unless we connect to the store ourselves or save isValid in the state based on the data in onUpdate.

@caesay caesay closed this Jun 21, 2017
@caesay caesay reopened this Jun 21, 2017
@davidkpiano
Copy link
Owner

I'll have to give this a tiny bit more thought. Are there use-cases where developers expect actions.submit(model) to force-submit the form, no matter what?

@caesay
Copy link
Contributor Author

caesay commented Jun 21, 2017

So it seems that currently onSubmit is always ignored if the form is invalid, and action is always executed no matter what.

Maybe it would be better to move some things around, here's some suggestions:

  • onBeforeSubmit could pass fieldState along with the event to the caller, this would be a good place to make a decision on whether we want to submit or not (we could check the form state ourselves and preventDefault if we wish).
  • before calling onSubmit we could check defaultPrevented, and if it's true it could bail out of onSubmit for consistency with the action prop.
  • There could be a prop on the form preventInvalidSubmit which would default to true and would prevent both mechanisms by default.
  • if preventInvalidSubmit is false, onSubmit would now get executed on an invalid form if not handled/cancelled in onBeforeSubmit

If you tell me what you think is best I would be happy to implement it.

@davidkpiano
Copy link
Owner

The first option seems like the simplest way to go - hopefully, we can avoid adding extra props. In the future, we can consider something like actions.forceSubmit(model).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants